package business.global.pk.tdk;

import java.util.ArrayList;

import business.player.Player;
import business.player.PlayerMgr;
import business.player.Robot.RobotMgr;
import business.player.feature.PlayerCurrency;
import business.tdk.c2s.cclass.TDKSet_Pos;
import business.tdk.c2s.cclass.TDK_KanPai;
import business.tdk.c2s.cclass.TDK_PosEnd;
import cenum.PrizeType;
import cenum.mj.OpType;
import com.ddm.server.common.utils.CommMath;

/**
 * 手上的牌最优计算，需要改进
 *
 * @author Administrator
 */
public class TDKSetPos {
    // 玩家位置
    private int posID = 0;
    // 吹牛-一局游戏逻辑
    private TDKRoomSet set = null;
    // 玩家房间信息
    private TDKRoomPos roomPos = null;
    // 私有牌
    private ArrayList<Integer> privateCards = new ArrayList<>();
    // 盖上的牌
    private ArrayList<Integer> coverCards = new ArrayList<>();
    // 是否弃权
    private boolean isQiPai;
    // 是否看牌
    private boolean isKanPai;
    // 筹码
    private int chip = 0;
    // 是否接锅
    private boolean isJieGuo = false;

    //最后一张是否是借的
    private boolean isJiepai =false;

    //本论踢或者不踢
    private  TDKdefine.TDK_OP  curTiOrBuTi=null;

    public TDKdefine.TDK_OP getCurTiOrBuTi() {
        return curTiOrBuTi;
    }

    public void setCurTiOrBuTi(TDKdefine.TDK_OP curTiOrBuTi) {
        this.curTiOrBuTi = curTiOrBuTi;
    }

    private int setPoint = 0;

    public TDKSetPos(int posID, TDKRoomPos roomPos, TDKRoomSet set) {
        this.posID = posID;
        this.roomPos = roomPos;
        this.set = set;
    }

    public TDKSetPos(int posID, TDKRoomSet set) {
        this.posID = posID;
        this.set = set;
    }


    public void clean() {
        this.set = null;
        this.roomPos = null;
        this.privateCards = null;
        this.coverCards = null;
    }

    /**
     * 清空牌
     */
    public void clearCards() {
        this.privateCards.clear();
        this.coverCards.clear();
        this.isQiPai = false;
        this.isKanPai = false;
    }

    public void clearKickOut(){
        this.privateCards.clear();
        this.coverCards.clear();
        this.isQiPai = false;
        this.isKanPai = false;
        this.chip = 0;
        this.isJieGuo = false;
        this.curTiOrBuTi=null;
        this.setPoint = 0;
    }

    public TDKRoomPos getRoomPos() {
        return roomPos;
    }

    public TDKSetPos(int posID) {
        this.posID = posID;
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public long getPid() {
        return this.roomPos.getPid();
    }

    /**
     * 获取位置ID
     *
     * @return
     */
    public int getPosID() {
        return posID;
    }

    /**
     * 检查玩家自动操作
     *
     * @return
     */
    public boolean checkPosRobot() {
        return true;
    }

    /**
     * 初始牌
     *
     * @param privateCards
     */
    public void init(ArrayList<Integer> privateCards) {
        this.privateCards.addAll(privateCards);
        if (!this.roomPos.isPlayTheGame()) {
            this.roomPos.setPlayTheGame(true);
        }
        this.coverCards.addAll(privateCards);
    }

    /**
     * 添加牌
     *
     * @param cardID
     */
    public void addCard(Integer cardID) {
        this.privateCards.add(cardID);
    }

    /**
     * 获取牌
     *
     * @return
     */
    public ArrayList<Integer> getPrivateCards() {
        return this.privateCards;
    }

    public ArrayList<Integer> getCardList(TDKdefine.TDK_CALC_TYPE calcType) {
        if (TDKdefine.TDK_CALC_TYPE.TDK_CALC_ALL.equals(calcType)) {
            return this.getPrivateCards();
        } else if (TDKdefine.TDK_CALC_TYPE.TDK_CALC_MING.equals(calcType)) {
            return this.mingCardList();
        } else if (TDKdefine.TDK_CALC_TYPE.TDK_CALC_FIVE.equals(calcType)) {
            ArrayList<Integer> fiveList = new ArrayList<>();
            fiveList.add(this.privateCards.get(this.sizePrivateCard() - 1));
            return fiveList;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 获取指定下标的牌
     *
     * @return
     */
    public int getIdxCard(int idx) {
        return this.privateCards.get(this.sizePrivateCard() - idx);
    }


    /**
     * 获取数字
     *
     * @return
     */
    public int sizePrivateCard() {
        return this.privateCards.size();
    }

    /**
     * 添加单个牌
     *
     * @param card
     */
    public void init(Integer card) {
        this.privateCards.add(card);
    }

    /**
     * 检查该玩家是否过
     *
     * @return
     */
    public int isPass() {
        return -1;
    }

    /**
     * 明牌列表
     *
     * @return
     */
    public ArrayList<Integer> mingCardList() {
        int length = this.privateCards.size();
        ArrayList<Integer> cardList = new ArrayList<>();
        Integer cardID = 0;
        for (int i = 0; i < length; i++) {
            cardID = this.privateCards.get(i);
            if (!this.coverCards.contains(cardID)) {
                cardList.add(cardID);
            }
        }
        return cardList;
    }

    /**
     * 身上牌显示
     *
     * @param isSelf T:可见，F:不可
     * @return
     */
    public TDKSet_Pos getNotify(boolean isSelf) {
        TDKSet_Pos ret = new TDKSet_Pos();
        // 玩家位置
        ret.posID = this.posID;
        // 玩家ID
        ret.pid = this.roomPos.getPid();
        // 是自己
        int length = this.privateCards.size();
        Integer cardID = 0;
        for (int i = 0; i < length; i++) {
            // 检查玩家是否弃牌
//            if (this.isQiPai) {
//                ret.privateCards.add(0);
//            } else {
                cardID = this.privateCards.get(i);
                //isSelf && this.isKanPai
                if (isSelf) {
                    ret.privateCards.add(cardID);
                } else if (this.coverCards.contains(cardID)) {
                    ret.privateCards.add(0);
                } else {
                    ret.privateCards.add(cardID);
                }
//            }
        }

        ret.isJiepai=this.isJiepai;
        ret.isKanPai = this.isKanPai;
        ret.isQiPai = this.isQiPai;
        // 分数
        ret.point = this.roomPos.getPoint();
        // 筹码
        ret.chip = this.chip;
        // 值
        ret.value = TDKSetPosOp.CountCardListValue(ret.privateCards, set.getDai2Flag());
        return ret;
    }

    /**
     * 所有人可见
     *
     * @return
     */
    public TDKSet_Pos getNotify() {
        TDKSet_Pos ret = new TDKSet_Pos();
        // 玩家位置
        ret.posID = this.posID;
        // 玩家ID
        ret.pid = this.roomPos.getPid();
        // 是自己
        int length = this.privateCards.size();
        Integer cardID = 0;
        for (int i = 0; i < length; i++) {
            // 检查玩家是否弃牌
            if (this.isQiPai) {
                ret.privateCards.add(0);
            } else {
                cardID = this.privateCards.get(i);
                ret.privateCards.add(cardID);
            }
        }
        ret.isKanPai = this.isKanPai;
        ret.isQiPai = this.isQiPai;
        // 分数
        ret.point = this.roomPos.getPoint();
        // 筹码
        ret.chip = this.chip;
        // 值
        ret.value = TDKSetPosOp.CountCardListValue(ret.privateCards, set.getDai2Flag());
        return ret;
    }


    /**
     * 回放记录
     *
     * @return
     */
    public TDKSet_Pos getNotifyPlayBack() {
        TDKSet_Pos ret = new TDKSet_Pos();
        // 玩家位置
        ret.posID = this.posID;
        // 玩家ID
        ret.pid = this.roomPos.getPid();
        ArrayList<Integer> privateCards = new ArrayList<>(); // 牌

        // 是自己
        int length = this.privateCards.size();
        Integer cardID = 0;
        for (int i = 0; i < length; i++) {
            // 检查玩家是否弃牌
            if (this.isQiPai) {
                privateCards.add(0);
                cardID = this.privateCards.get(i);
                ret.privateCards.add(cardID);
            } else {
                cardID = this.privateCards.get(i);
                ret.privateCards.add(cardID);
                privateCards.add(cardID);
            }
        }
        ret.isKanPai = this.isKanPai;
        ret.isQiPai = this.isQiPai;
        // 分数
        ret.point = this.roomPos.getPoint();
        // 筹码
        ret.chip = this.chip;
        // 值
        ret.value = TDKSetPosOp.CountCardListValue(privateCards, set.getDai2Flag());
        ret.isLostConnect = new Boolean(this.roomPos.isLostConnect());
        ret.setTrusteeship(this.roomPos.isTrusteeship());
        return ret;
    }


    /**
     * 看牌
     *
     * @return
     */
    public TDK_KanPai kanPai() {
        TDK_KanPai kanPai = new TDK_KanPai();
        TDKSet_Pos setPos = getNotify(true);
        kanPai.setPrivateCards(setPos.privateCards);
        kanPai.setValue(TDKSetPosOp.CountCardListValue(setPos.privateCards, set.getDai2Flag()));
        return kanPai;
    }


    /**
     * 设置当局分数
     *
     * @param setPoint
     */
    public void setSetPoint(int setPoint) {
        this.setPoint = setPoint;
    }


    /**
     * 获取当局分数
     *
     * @return
     */
    public int getSetPoint() {
        return setPoint;
    }

    /**
     * 计算位置结算
     *
     * @return
     */
    public TDK_PosEnd calcPosEnd() {
        TDK_PosEnd posEnd = new TDK_PosEnd();
        posEnd.setPos(this.posID);
        posEnd.setPid(this.roomPos.getPid());
        this.roomPos.setCount(setPoint);
        this.roomPos.calcRoomPoint(setPoint);
        //底注
        int dizhu = TDKdefine.TDK_DIFEN_TYPE.getFirstChipSet(this.set.getRoom().getDiFen(),this.set.getLanguoBeishu());
        if(this.roomPos.getRoomSportsPointValue() <= CommMath.FormatDouble(this.getRoomPos().getRoom().getBaseRoomConfigure().getBaseCreateRoom().getAutoDismiss()) || this.roomPos.getRoomSportsPointValue() < CommMath.FormatDouble(dizhu)){
            this.roomPos.setLeaveNextSet(true);
        }
        posEnd.setPoint(this.roomPos.getPoint());
        /*
        if (this.set.isAllQiPai() && !this.isQiPai) {
            posEnd.setPrivateCards(getNotify(false).privateCards);
            posEnd.setShowValue(false);
        } else {
            posEnd.setShowValue(true);
            posEnd.setPrivateCards(this.privateCards);
        }
         */
        //YSJVB
        posEnd.setShowValue(true);
        posEnd.setPrivateCards(this.privateCards);
        posEnd.setJiepai(this.isJiepai);
        posEnd.setSetPoint(setPoint);
        posEnd.setValue(TDKSetPosOp.CountCardListValue(this.privateCards, set.getDai2Flag()));
        posEnd.setIsOwner(this.roomPos.getPid() == this.set.getRoom().getOwnerID() ? 1 : 0);
        posEnd.setQiPai(this.isQiPai);
        posEnd.setSportsPoint(this.roomPos.setSportsPoint(setPoint));
        if (set.getRoom().getBaseRoomConfigure().getPrizeType() == PrizeType.Gold) {
            posEnd.setSetPoint(setPoint * this.set.getRoom().getBaseMark());
            if (!RobotMgr.getInstance().isRobot((int) getPid())) {
                Player player = PlayerMgr.getInstance().getPlayer(getPid());
                player.getFeature(PlayerCurrency.class).goldRoomEnd(posEnd.getSetPoint(), this.set.getRoom().getBaseMark(), this.set.getRoom().getBaseRoomConfigure().getGameType().getId());
            }
        }
        posEnd.setJieGuo(this.isJieGuo());
        if (this.isJieGuo()) {
            // 统计接锅次数
            this.roomPos.addJieGuoCount();
        }
        return posEnd;
    }

    /**
     * 是否弃牌
     *
     * @return
     */
    public boolean isQiPai() {
        return isQiPai;
    }

    /**
     * 设置弃牌
     *
     * @param isQiPai T:确认弃牌。
     */
    public void setQiPai(boolean isQiPai) {
        if (isQiPai) {
            this.set.setQiPaiPos(this.getPosID());
        }
        this.isQiPai = isQiPai;
    }


    /**
     * 设置是否看牌
     *
     * @param isKanPai
     */
    public void setKanPai(boolean isKanPai) {
        this.isKanPai = !this.isKanPai;
    }


    /**
     * 是否看牌
     *
     * @return
     */
    public boolean isKanPai() {
        return isKanPai;
    }

    /**
     * 计算玩家分数
     */
    public void calcPosPoint() {

    }

    /**
     * 获取筹码
     *
     * @return
     */
    public int getChip() {
        return chip;
    }

    /**
     * 增加筹码
     *
     * @param chip
     */
    public void addChip(int chip) {
        this.chip += chip;
    }

    /**
     * 是否接锅
     *
     * @return
     */
    public boolean isJieGuo() {
        return isJieGuo;
    }

    /**
     * 设置接锅
     *
     * @param isJieGuo
     */
    public void setJieGuo(boolean isJieGuo) {
        this.isJieGuo = isJieGuo;
    }

    public boolean isJiepai() {
        return isJiepai;
    }

    public void setJiepai(boolean jiepai) {
        isJiepai = jiepai;
    }
}
